unit uCreateTypedEntity;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Oracle, StdCtrls, Buttons, DB, OracleData;

type
  TfmCreateTypedEntity = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    edName: TEdit;
    cbEntityType: TComboBox;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    odsSelectAvaliableEntityTypes: TOracleDataSet;
    procedure cbEntityTypeChange(Sender: TObject);
  private
    { Private declarations }
    procedure ClearFields;
  public
    { Public declarations }

    TypeId : string;
    Name : string;

    function Execute(WindowCaption : string; entitityTypeTableName : string; entityTypePrimaryKey : string):boolean;
  end;

var
  fmCreateTypedEntity: TfmCreateTypedEntity;

implementation

{$R *.dfm}

uses uMain;

procedure TfmCreateTypedEntity.ClearFields;
begin
  edName.Text := '';
  cbEntityType.Items.Clear;
end;

function TfmCreateTypedEntity.Execute(WindowCaption : string; entitityTypeTableName : string; entityTypePrimaryKey : string):boolean;
begin
  result := false;

  Caption := WindowCaption;
  ClearFields;

  odsSelectAvaliableEntityTypes.Close;
  odsSelectAvaliableEntityTypes.sql.Text := 'select '+entityTypePrimaryKey+' as id, name from '+entitityTypeTableName;
  odsSelectAvaliableEntityTypes.Open;

  odsSelectAvaliableEntityTypes.First;

  while not (odsSelectAvaliableEntityTypes.eof) do
  begin
    cbEntityType.Items.AddObject(odsSelectAvaliableEntityTypes.FieldByName('NAME').AsString, TObject(odsSelectAvaliableEntityTypes.FieldByName('ID').AsInteger));

    odsSelectAvaliableEntityTypes.Next;
  end;

  odsSelectAvaliableEntityTypes.Close;

  BitBtn1.Enabled := false;

  if (ShowModal = mrOk) then
  begin
    Name := edName.Text;
    TypeId := IntToStr(integer(cbEntityType.Items.Objects[cbEntityType.ItemIndex]));

    result := true;
  end;
end;

procedure TfmCreateTypedEntity.cbEntityTypeChange(Sender: TObject);
begin
  BitBtn1.Enabled := true;
end;

end.
